package cn.openea.perpartner.meily.web.controller;


import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.utils.poi.ExcelUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.util.UserContext;
import cn.openea.perpartner.meily.web.entity.Category;
import cn.openea.perpartner.meily.web.entity.dto.CategoryDto;
import cn.openea.perpartner.meily.web.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Category)表控制层
 *
 * @author Weird077
 * @since 2023-01-11 21:04:18
 */
@Api(tags = "商品分类")
@RestController
@RequestMapping("/goods/category")
public class CategoryController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ICategoryService categoryService;

    @ApiOperation("商品分类列表")
    @GetMapping("/list/{parentId}")
    public TableDataInfo list(@PathVariable String parentId){
        List<CategoryDto> list = categoryService.selectList(parentId);
        return  getDataTable(list);
    }
    @ApiOperation("添加商品分类")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Category category) {
        if (StrUtil.isBlank(category.getParentId())){
            category.setParentId("root");
        }
        category.setCreateUser(UserContext.getUserId());
        category.setUpdateUser(UserContext.getUserId());
        return toAjax(this.categoryService.save(category));
    }
    @ApiOperation("修改商品分类")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Category category) {
        category.setUpdateUser(UserContext.getUserId());
        return toAjax(this.categoryService.updateById(category));
    }
    @ApiOperation("商品分类详情")
    @GetMapping("/{id}}")
    public AjaxResult getOne(@PathVariable String id) {
        return AjaxResult.success(this.categoryService.getById(id));
    }
    @ApiOperation("导入商品分类")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        List<Category> categoryList = util.importExcel(file.getInputStream());
        return toAjax(categoryService.importData(categoryList));
    }
}

